package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.mapper.SysMenuMapper;
import com.sofwin.mapper.SysRoleMapper;
import com.sofwin.mapper.SysRoleMenuMapper;
import com.sofwin.pojo.SysRole;
import com.sofwin.pojo.SysRoleExample;
import com.sofwin.pojo.SysRoleMenu;
import com.sofwin.pojo.SysRoleMenuExample;
import com.sofwin.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @packageName: com.sofwin.service.impl
 * @author: andyliu
 * @date: 2022/8/12 9:36
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private SysRoleMapper mapper;

    @Autowired
    private SysMenuMapper menuMapper;

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    @Override
    public boolean saveRole(SysRole role, Integer[] menuIds) {
        int flag2 = 0;
        if(role.getId()==null) {
            // 保存角色信息
            int flag = mapper.insertSelective(role);
            if(menuIds!=null && menuIds.length>0) {
                for (Integer menuId : menuIds
                ) {
                    SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setRoleId(role.getId());
                    roleMenu.setMenuId(menuId);
                    flag2 += roleMenuMapper.insert(roleMenu);
                }
            }
            return (flag == 1 && flag2 == menuIds.length) ? true : false;
        }
        int flag = mapper.updateByPrimaryKeySelective(role);
        // 删除该角色的所有按钮
        // 再新增 -- 没有任何权限
        SysRoleMenuExample example = new SysRoleMenuExample();
        SysRoleMenuExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(role.getId());
        roleMenuMapper.deleteByExample(example);
        if(menuIds!=null && menuIds.length>0) {
            for (Integer menuId : menuIds
            ) {
                SysRoleMenu roleMenu = new SysRoleMenu();
                roleMenu.setRoleId(role.getId());
                roleMenu.setMenuId(menuId);
                flag2 += roleMenuMapper.insert(roleMenu);
            }
        }
        return (flag == 1 && flag2 == menuIds.length) ? true : false;

    }

    @Override
    public boolean removeRoleById(Integer id) {
        return false;
    }

    @Override
    public boolean removeRoleByIds(Integer[] ids) {
        return false;
    }

    @Override
    public SysRole queryById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SysRole> queryByPage(SysRole role, Integer pageNumber, Integer pageSize) {
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(role.getRoleName())){
            criteria.andRoleNameLike("%"+role.getRoleName()+"%");
        }
        PageHelper.startPage(pageNumber,pageSize);
        List<SysRole> roles = mapper.selectByExample(example);
        return new PageInfo<>(roles);
    }
}
